package com.iyue.servlet.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 
 * 
 * @author 孙晓晔
 * @version $Id: RequestFilter.java,v 1.1 2010/10/14 17:07:14 fresh Exp $
 */
public final class RequestFilter implements Filter {
	/**
	 * The filter configuration object we are associated with. If this value is
	 * null, this filter instance is not currently configured.
	 */
	private FilterConfig filterConfig = null;

	/**
	 * Take this filter out of service.
	 */
	public void destroy() {
		filterConfig = null;
	}

	/**
	 * Time the processing that is performed by all subsequent filters in the
	 * current filter stack, including the ultimately invoked servlet.
	 * 
	 * @param request
	 *            The servlet request we are processing
	 * @param response
	 *            The servlet response we are creating
	 * @param chain
	 *            The filter chain we are processing
	 * 
	 * @exception IOException
	 *                if an input/output error occurs
	 * @exception ServletException
	 *                if a servlet error occurs
	 */
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {

		if (filterConfig == null) {
			return;
		}

		if (request instanceof HttpServletRequest) {
			if (aa((HttpServletRequest) request, (HttpServletResponse) response)) {
				return;
			}
		}

		chain.doFilter(request, response);
	}

	@SuppressWarnings("unchecked")
	private boolean aa(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		if (!request.getMethod().equalsIgnoreCase("post")) {
			return false;
		}

		if (check(request)) {
			RequestDispatcher rd = request
					.getRequestDispatcher("/portal.do?do=Portal.duplicate");
			rd.forward(request, response);
			return true;
		}

		// Keyword key = new Keyword();
		// Enumeration<String> names = request.getParameterNames();
		// String name;
		// while (names.hasMoreElements()) {
		// name = (String) names.nextElement();
		// if (!key.isCheck(name)) {
		// continue;
		// }
		// key.setData(request.getParameter(name));
		// if (key.isIllegal()) {
		// RequestDispatcher rd = request
		// .getRequestDispatcher("/portal.do?do=Portal.illegal");
		// rd.forward(request, response);
		// return true;
		// }
		// }

		return false;
	}

	/**
	 * 检测提交数据时间间隔，暂定为2秒
	 * 
	 * @param request
	 * @param s
	 * @return
	 */
	private boolean check(HttpServletRequest request) {
		String lasttime = "_lasttime";

		long currentTimeMillis = System.currentTimeMillis();
		Long last = Long.valueOf(currentTimeMillis);

		HttpSession session = request.getSession();
		Long time = (Long) session.getAttribute(lasttime);
		if (time == null) {
			session.setAttribute(lasttime, last);
			return false;
		}

		if (currentTimeMillis - time.longValue() > 2000) {
			session.setAttribute(lasttime, last);
			return false;
		}

		return true;
	}

	/**
	 * Place this filter into service.
	 * 
	 * @param filterConfig
	 *            The filter configuration object
	 */
	public void init(FilterConfig filterConfig) throws ServletException {
		this.filterConfig = filterConfig;
	}

	/**
	 * Return a String representation of this object.
	 */
	public String toString() {

		if (filterConfig == null) {
			return "RequestFilter()";
		}

		StringBuffer sb = new StringBuffer("RequestFilter(");
		sb.append(filterConfig);
		sb.append(")");

		return (sb.toString());
	}

}
